﻿
using System.Configuration;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using EntitiesAndMappings.Entities;

namespace ProyectoActivos.Daos
{
    public class SessionFactoryHelper
    {
        private static ISessionFactory sessionFactory;

        public static ISessionFactory GetSessionFactory()
        {
            if (sessionFactory == null)
            {

                AppSettingsReader reader = new AppSettingsReader();

                sessionFactory = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2005
                                  .ConnectionString(c => c.Server(((string)(reader.GetValue("Server", typeof(string)))))
                                                          .Database(((string)(reader.GetValue("Database", typeof(string)))))
                                                          .Username(((string)(reader.GetValue("Username", typeof(string)))))
                                                          .Password(((string)(reader.GetValue("Password", typeof(string)))))
                                  ))
                    .ExposeConfiguration(BuildSchema)
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Usuario>())
                    .Mappings(m => m.HbmMappings.AddFromAssemblyOf<Usuario>())
                    .BuildSessionFactory();

            }

            return sessionFactory;
        }


        private static void BuildSchema(Configuration config)
        {
          //  new NHibernate.Tool.hbm2ddl.SchemaExport(config).Execute(true, true, false);
        }
    }
}
